home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / optimr.zip / BENCH.PAS next >
Pascal/Delphi Source File  |  1992-09-30  |  7KB  |  306 lines

  1. {$A+}
  2. program Bench;
  3.  
  4. uses
  5.   OpTimer,
  6.   OpString,
  7.   OpRoot,
  8.   OpCrt,
  9.   OpDos,
  10.   OpLArray;
  11.  
  12. const
  13.   ArrayNames : Array[lRamArray..lVirtualArray] of String[7] = ('RAM',
  14.                                                                'EMS',
  15.                                                                'VIRTUAL');
  16.  
  17.   {$IFDEF Dpmi}
  18.   RPriorities : AutoPriority = (lRamArray,lNoArray);
  19.   VPriorities : AutoPriority = (lVirtualArray,lNoArray);
  20.   APriorities : AutoPriority = (lRamArray,lVirtualArray);
  21.   {$ELSE}
  22.   RPriorities : AutoPriority = (lRamArray,lNoArray,lNoArray);
  23.   EPriorities : AutoPriority = (lEMSArray,lNoArray,lNoArray);
  24.   VPriorities : AutoPriority = (lVirtualArray,lNoArray,lNoArray);
  25.   APriorities : AutoPriority = (lRamArray,lEMSArray,lVirtualArray);
  26.   {$ENDIF}
  27.  
  28. type
  29.   Str80 = String[80];
  30.   ByteArray    = Array[1..11] of Array[1..11] of Byte;
  31.   WordArray    = Array[1..11] of Array[1..11] of Word;
  32.   RealArray    = Array[1..11] of Array[1..11] of Real;
  33.   StrArray     = Array[1..11] of Array[1..11] of Str80;
  34.  
  35. var
  36.   Priorities : AutoPriority;
  37. var
  38.   Arr : OpArrayPtr;
  39. var
  40.   HeapToUse,Start,Stop,EmptyLoopTime : LongInt;
  41. var
  42.   RowNum,ColNum,MaximumRows,MaximumCols,ElementSize : Word;
  43. var
  44.   BA : ByteArray;
  45. var
  46.   WA : WordArray;
  47. var
  48.   RA : RealArray;
  49. var
  50.   SA : StrArray;
  51. var
  52.   AType : ArrayType;
  53. var
  54.   NumToDo : Word;
  55.  
  56. procedure Abort(A : OpArrayPtr);
  57.  
  58. begin
  59.   Dispose(A,Done);
  60.   Halt;
  61. end;
  62.  
  63. function GetArrayType : ArrayType;
  64.  
  65. var
  66.   C : Char;
  67.  
  68. begin
  69.   {$IFDEF Dpmi}
  70.   Write('Enter Array Type (R,V,A): ');
  71.   repeat
  72.     C := UpCase(ReadKey);
  73.   until C in ['R','V','A',^[];
  74.   {$ELSE}
  75.   Write('Enter Array Type (R,E,V,A): ');
  76.   repeat
  77.     C := UpCase(ReadKey);
  78.   until C in ['R','E','V','A',^[];
  79.   {$ENDIF}
  80.   case C of
  81.     'R' : begin
  82.             Priorities := RPriorities;
  83.             GetArrayType := lRamArray;
  84.           end;
  85.     {$IFNDEF Dpmi}
  86.     'E' : begin
  87.             Priorities := EPriorities;
  88.             GetArrayType := lEMSArray;
  89.           end;
  90.     {$ENDIF}
  91.     'V' : begin
  92.             Priorities := VPriorities;
  93.             GetArrayType := lVirtualArray;
  94.           end;
  95.     'A' : begin
  96.             Priorities := APriorities;
  97.             GetArrayType := lNoArray;
  98.           end;
  99.     else Halt;
  100.   end;
  101.   WriteLn(C);
  102. end;
  103.  
  104. function GetElementType : Word;
  105.  
  106. var
  107.   C : Char;
  108.  
  109. begin
  110.   Write('Enter Element Type (Byte,Word,Real,String): ');
  111.   repeat
  112.     C := UpCase(ReadKey);
  113.   until C in ['B','W','R','S',^[];
  114.   case C of
  115.     'B' : GetElementType := SizeOf(Byte);
  116.     'W' : GetElementType := SizeOf(Word);
  117.     'R' : GetElementType := SizeOf(Real);
  118.     'S' : GetElementType := SizeOf(Str80);
  119.     else Halt;
  120.   end;
  121.   WriteLn(C);
  122. end;
  123.  
  124. function GetAWord(S : String) : Word;
  125. var
  126.   NStr : String;
  127.   N : Word;
  128.  
  129. begin
  130.   Write(S);
  131.   ReadLn(NStr);
  132.   if (not Str2Word(NStr,N)) or (N = 0) then
  133.     Halt;
  134.   GetAWord := N;
  135. end;
  136.  
  137. procedure GetMaxRowCol;
  138.  
  139. begin
  140.   MaximumRows := GetAWord('Enter max rows: ');
  141.   MaximumCols := GetAWord('Enter max cols: ');
  142. end;
  143.  
  144. procedure GetNumToDo;
  145.  
  146. begin
  147.   NumToDo := GetAWord('Enter number to do: ');
  148. end;
  149.  
  150. procedure GetRowCol;
  151.  
  152. begin
  153.   RowNum := GetAWord('Enter row to read: ');
  154.   ColNum := GetAWord('Enter col to read: ');
  155. end;
  156.  
  157. function YesNo(Msg : String) : Boolean;
  158. var
  159.   C : Char;
  160.  
  161. begin
  162.   Write(Msg);
  163.   repeat
  164.     C := UpCase(ReadKey);
  165.   until C in ['Y','N'];
  166.   WriteLn(C);
  167.   YesNo := C = 'Y';
  168. end;
  169.  
  170. function CalcElapsedTime(Start,Stop : LongInt) : String;
  171. var
  172.   NumMs : Real;
  173.   S : String;
  174. begin
  175.   Stop := Stop - EmptyLoopTime;
  176.   NumMs := 1000*ElapsedTime(Start,Stop)/NumToDo;
  177.  
  178.   Str(NumMs:8:2,S);
  179.   CalcElapsedTime := S;
  180. end;
  181.  
  182. procedure DoByteTest;
  183.  
  184. var
  185.   I : Word;
  186.   B : Byte;
  187. begin
  188.   WriteLn('Performing byte test');
  189.   Start := ReadTimer;
  190.   for I := 1 to NumToDo do
  191.     Arr^.RetA(RowNum,ColNum,B);
  192.   Stop := ReadTimer;
  193.   WriteLn('Time for OpLarray = ',CalcElapsedTime(Start,Stop));
  194.   Start := ReadTimer;
  195.   for I := 1 to NumToDo do
  196.     B := BA[RowNum,ColNum];
  197.   Stop := ReadTimer;
  198.   WriteLn('Time for Turbo    = ',CalcElapsedTime(Start,Stop));
  199. end;
  200.  
  201. procedure DoWordTest;
  202.  
  203. var
  204.   W,I : Word;
  205. begin
  206.   WriteLn('Performing word test');
  207.   Start := ReadTimer;
  208.   for I := 1 to NumToDo do
  209.     Arr^.RetA(RowNum,ColNum,W);
  210.   Stop := ReadTimer;
  211.   WriteLn('Time for OpLarray = ',CalcElapsedTime(Start,Stop));
  212.   Start := ReadTimer;
  213.   for I := 1 to NumToDo do
  214.     W := WA[RowNum,ColNum];
  215.   Stop := ReadTimer;
  216.   WriteLn('Time for Turbo    = ',CalcElapsedTime(Start,Stop));
  217. end;
  218.  
  219. procedure DoRealTest;
  220.  
  221. var
  222.   I : Word;
  223.   R : Real;
  224. begin
  225.   WriteLn('Performing real test');
  226.   Start := ReadTimer;
  227.   for I := 1 to NumToDo do
  228.     Arr^.RetA(RowNum,ColNum,R);
  229.   Stop := ReadTimer;
  230.   WriteLn('Time for OpLarray = ',CalcElapsedTime(Start,Stop));
  231.   Start := ReadTimer;
  232.   for I := 1 to NumToDo do
  233.     R := RA[RowNum,ColNum];
  234.   Stop := ReadTimer;
  235.   WriteLn('Time for Turbo    = ',CalcElapsedTime(Start,Stop));
  236. end;
  237.  
  238. procedure DoStringTest;
  239.  
  240. var
  241.   I : Word;
  242.   S : Str80;
  243. begin
  244.   WriteLn('Performing string test');
  245.   Start := ReadTimer;
  246.   for I := 1 to NumToDo do
  247.     Arr^.RetA(RowNum,ColNum,S);
  248.   Stop := ReadTimer;
  249.   WriteLn('Time for OpLarray = ',CalcElapsedTime(Start,Stop));
  250.   Start := ReadTimer;
  251.   for I := 1 to NumToDo do
  252.     S := SA[RowNum,ColNum];
  253.   Stop := ReadTimer;
  254.   WriteLn('Time for Turbo    = ',CalcElapsedTime(Start,Stop));
  255. end;
  256.  
  257. procedure Benchmark;
  258. var
  259.   Z : String;
  260. begin
  261.   if YesNo('Read to force page? ') then
  262.     Arr^.RetA(RowNum,ColNum,Z);
  263.   case ElementSize of
  264.     SizeOf(Byte)   : DoByteTest;
  265.     SizeOf(Word)   : DoWordTest;
  266.     SizeOf(Real)   : DoRealTest;
  267.     SizeOf(Str80)  : DoStringTest;
  268.   end;
  269. end;
  270.  
  271. function TimeEmptyLoop : LongInt;
  272. var
  273.   I : Word;
  274. begin
  275.   Start := ReadTimer;
  276.   for I := 1 to NumToDo do begin
  277.   end;
  278.   Stop := ReadTimer;
  279.   TimeEmptyLoop := Stop - Start;
  280. end;
  281.  
  282. begin
  283.   FillChar(SA,SizeOf(SA),80);
  284.   AType := GetArrayType;
  285.   GetMaxRowCol;
  286.   GetRowCol;
  287.   GetNumToDo;
  288.   ElementSize := GetElementType;
  289.   if AType in [lNoArray,lRamArray,lVirtualArray] then
  290.     HeapToUse := MaxAvail - 10000
  291.   else
  292.     HeapToUse := 1;
  293.   EmptyLoopTime := TimeEmptyLoop;
  294.   WriteLn('Time for an empty loop ',EmptyLoopTime);
  295.   Arr := New(OpArrayPtr, Init(MaximumRows,MaximumCols,
  296.                               ElementSize,'BENCH.DAT',
  297.                               HeapToUse,0,Priorities));
  298.   if Arr = NIL then begin
  299.     WriteLn('Unable to allocate array');
  300.     Halt;
  301.   end;
  302.   WriteLn('The element size is ', Arr^.GetElementSize);
  303.   Benchmark;
  304.   Dispose(Arr,Done);
  305. end.
  306.